package com.stupidzhe.vbreview.util;

import com.stupidzhe.vbreview.bean.*;
import com.stupidzhe.vbreview.exception.AlgorithmException;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.*;

import static com.stupidzhe.vbreview.util.FileUtil.FILE_PATH;

/**
 * 解析VB文件相关工具类
 */
public class VBAssUtil {

    private static final String VB_PATH = "C:\\Users\\StupidZhe\\VB98\\";

    private static final String errLocation = "C:\\Users\\StupidZhe\\Documents\\vb-review\\errInfo.log";
//    private static final String errLocation = "/Users/StupidZhe/IT/Documents/vb-review/errInfo.log";

    private static Process pp;

    /**
     * 解析vbp文件
     */
    public static VbpStructure analyzeVbp(File file) {
        String fileName = file.getName();
        VbpStructure vbpStructure = new VbpStructure();
        if (!"vbp".equals(fileName.split("[.]")[1])) {
            return null;
        }
        String content = null;
        String[] keyVal = null;
        try {
            FileReader fileReader = new FileReader(file, Charset.forName("GBK"));
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            while (null != (content = bufferedReader.readLine())) {
                System.out.println(content);
                keyVal = content.split("[=]");
                if (keyVal.length == 2) {
                    if ("FavorPentiumPro(tm)".equals(keyVal[0])) {
                        keyVal[0] = "FavorPentiumPro";
                    }
                    try {
                        Field attr = vbpStructure.getClass().getDeclaredField(keyVal[0]);
                        attr.set(vbpStructure, keyVal[1]);
                    } catch (NoSuchFieldException e) {
                        continue;
                    }
                }
            }
        } catch (IOException | IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
        return vbpStructure;
    }

    /**
     * 解析frm文件
     *
     * @param file
     * @return
     */
    public static FrmStructure analyzeFrm(File file) throws Exception {
        String fileName = file.getName();
        FrmStructure frmStructure = new FrmStructure();
        if (!"frm".equals(fileName.split("[.]")[1])) {
            return null;
        }
        String tmp;
        try {
            FileReader fileReader = new FileReader(file, Charset.forName("GBK"));
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            FrmStructure.Component component = null;
            FrmStructure.Sub sub = null;
            FrmStructure.Component form = null;
            while (null != (tmp = bufferedReader.readLine())) {
                System.out.println(tmp);
                tmp = tmp.split("[']")[0];
                if ("".equals(tmp.trim())) {
                    continue;
                }
                String[] tmp2 = tmp.split("[ ]");
                List<String> tmpList = new LinkedList<>();
                for (String tmpp : tmp2) {
                    if (!"".equals(tmpp)) {
                        tmpList.add(tmpp);
                    }
                }
                String[] words = new String[tmpList.size()];
                tmpList.toArray(words);

                // 判断是否为文件开头
                if ("VERSION".equals(words[0])) {
                    frmStructure.version = words[1];
                    continue;
                }

                // 判断Sub是否结束
                if (words.length == 2 && "End".equals(words[0]) && "Sub".equals(words[1])) {
                    frmStructure.subList.add(sub);
                    sub = null;
                    continue;
                }

                // 如果在过程体中
                if (sub != null) {
                    tmp = tmp.replaceAll("[(]", " ( ").
                            replaceAll("[)]", " ) ").
                            replaceAll("[+]", " + ").
                            replaceAll("[-]", " - ").
                            replaceAll("[*]", " * ").
                            replaceAll("[/]", " / ").
                            replaceAll("[=]", " = ").
                            replaceAll("[,]", " , ").
                            replaceAll("[)]", " ) ");
                    String tmp4 = "";
                    String[] tmp3 = tmp.split("[ ]");
                    for (String tmpp : tmp3) {
                        if (!"".equals(tmpp)) {
                            tmp4 += tmpp + " ";
                        }
                    }

                    sub.body += tmp4 + "\n";
                }

                // 判断组件说明是否结束
                if ((component != null || form != null) && "End".equals(words[0])) {
                    if (form != null) {
                        frmStructure.componentList.add(form);
                        form = null;
                    } else {
                        frmStructure.componentList.add(component);
                        component = null;
                        continue;
                    }
                }

                // 判断是否为Form开始
                if ("Begin".equals(words[0]) && "VB.Form".equals(words[1])) {
                    form = frmStructure.new Component();
                    form.type = words[1];
                    form.name = words[2];
                    continue;
                    // 判断是否为普通组件开始
                } else if ("Begin".equals(words[0])) {
                    if (form != null) {
                        frmStructure.componentList.add(form);
                        form = null;
                    }
                    // 书写格式不规范
                    if (component != null) {
                        throw new Exception(".frm文件错误：书写格式不规范\n（提示：\"Begin\",\"End\"无法正确匹配）");
                    }
                    component = frmStructure.new Component();
                    component.type = words[1];
                    component.name = words[2];
                    continue;
                }
                // 判断是否为Form的Caption
                if ("Caption".equals(words[0]) && form != null) {
                    form.caption = words[2];
                    continue;
                    // 判断是否为普通组件的Caption
                } else if ("Caption".equals(words[0])) {
                    if (component == null) {
                        throw new Exception(".frm文件错误：书写格式不规范\n（提示：\"Caption\"无法正确匹配）");
                    }
                    component.caption = words[2];
                }

                // 判断是否为属性
                if ("Attribute".equals(words[0])) {
                    frmStructure.Attribute.put(words[1], words[3]);
                    continue;
                }

                // 判断是否为Sub声明
                if (words.length > 2 && "Sub".equals(words[1])) {
                    sub = frmStructure.new Sub();
                    sub.modifier = words[0];
                    sub.name = words[2];
                    continue;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return frmStructure;
    }

    /**
     * VB项目分析
     *
     * @param projectLocation 学生作业地址
     * @param demoLocation    教师答案地址
     * @param classification  类别（实例题，设计题）
     * @return 结果
     */
    public static VBAssResultBean analyzeVB(String projectLocation, String demoLocation, String classification) {
        File demoDir = new File(FILE_PATH + demoLocation.split("[.]")[0]);
        File[] demoArr = demoDir.listFiles();
        FrmStructure frmDemo = null;
        VbpStructure vbpDemo = null;
        try {

            for (File demo : demoArr) {
                if ("frm".equalsIgnoreCase(demo.getName().split("[.]")[1])) {
                    frmDemo = analyzeFrm(demo);
                } else if ("vbp".equalsIgnoreCase(demo.getName().split("[.]")[1])) {
                    vbpDemo = analyzeVbp(demo);

                }
            }
        } catch (Exception e) {
            return new VBAssResultBean(false, "教师文件结构出错(1)");
        }
        if (frmDemo == null || vbpDemo == null) {
            return new VBAssResultBean(false, "教师文件结构出错(2)");
        }
        File projectDir = new File(FILE_PATH + projectLocation.split("[.]")[0]);
        File[] projectArr = projectDir.listFiles();
        FrmStructure frmProject = null;
        VbpStructure vbpProject = null;
        try {
            for (File demo : projectArr) {
                if ("frm".equalsIgnoreCase(demo.getName().split("[.]")[1])) {
                    frmProject = analyzeFrm(demo);
                } else if ("vbp".equalsIgnoreCase(demo.getName().split("[.]")[1])) {
                    vbpProject = analyzeVbp(demo);
                }
            }
        } catch (Exception e) {
            return new VBAssResultBean(false, e.getMessage());
        }

        if (frmProject == null || vbpProject == null) {
            return new VBAssResultBean(false, "学生上传作业结构出错");
        }

        // 如果是实例题
        if ("0".equals(classification)) {
            VBAssResultBean vbpResultBean = assVbp(vbpDemo, vbpProject);
            VBAssResultBean frmResultBean = assFrm(frmDemo, frmProject);
            if (!vbpResultBean.isStatus() && !frmResultBean.isStatus()) {
                return new VBAssResultBean(false, "1." + vbpResultBean.getResult() + ";2." + frmResultBean.getResult());
            }
            if (!vbpResultBean.isStatus()) {
                return vbpResultBean;
            }
            if (!frmResultBean.isStatus()) {
                return frmResultBean;
            }
            return new VBAssResultBean(true, "作业通过");

            // 如果是设计题
        } else {
            VBDAssResultBean resultBean = assFrmDe(frmDemo, frmProject);
            return new VBAssResultBean(resultBean.isStatus(), resultBean.getResult(), resultBean.getScore());
        }
    }

    /**
     * 实例题对比.frm文件
     *
     * @param demo    教师标准源代码
     * @param project 学生上传源代码
     * @return 对比结果
     */
    private static VBAssResultBean assFrm(FrmStructure demo, FrmStructure project) {
        for (FrmStructure.Component demoCom : demo.componentList) {
            boolean exist = false;
            boolean flag;
            for (FrmStructure.Component component : project.componentList) {
                // 组件类型与名称相同判断
                if (component.type.equals(demoCom.type) && component.name.equals(demoCom.name)) {
                    exist = true;
                    if (demoCom.caption != null) {
                        // 判断标题是否一致
                        flag = demoCom.caption.equalsIgnoreCase(component.caption.trim());
                        if (!flag) {
                            return new VBAssResultBean(false, ".frm文件错误：组件\"" + demoCom.name + "\"caption属性错误");
                        }
                    } else {
                        flag = true;
                    }
                }
            }
            if (!exist) {
                return new VBAssResultBean(false, ".frm文件错误：组件\"" + demoCom.name + "\"不存在");
            }
        }

        // 实例题检验Sub（要求一一对应）
        boolean exist = false;
        for (FrmStructure.Sub demoSub : demo.subList) {
            for (FrmStructure.Sub sub : project.subList) {
                if (demoSub.modifier.equals(sub.modifier) && demoSub.name.equals(sub.name)) {

                    // 判断重复
                    if (exist) {
                        return new VBAssResultBean(false, ".frm文件错误：组件\"" + demoSub.modifier + " " + demoSub.name + "\"重复");
                    }
                    exist = true;

                    // 通过" "分割字符串
                    String[] demoBody = demoSub.body.replace("\n", "").split("[ ]");
                    String[] projectBody = sub.body.replace("\n", "").split("[ ]");
                    for (int n = 0; n < demoBody.length; n++) {
                        if (!demoBody[n].equals(projectBody[n])) {
                            return new VBAssResultBean(false, ".frm文件错误：Sub\"" +
                                    demoSub.modifier + " " + demoSub.name + "\"错误：\"" + projectBody[n] + "\"");
                        }

                    }
                }
            }
            if (!exist) {
                return new VBAssResultBean(false, ".frm文件错误：Sub\"" + demoSub.modifier + " " + demoSub.name + "\"不存在");
            }
            exist = false;
        }
        return new VBAssResultBean(true, ".frm文件正确");
    }

    /**
     * 实例题对比.vbp文件
     *
     * @param demo    教师标准源代码
     * @param project 学生上传源代码
     * @return 对比结果
     */
    private static VBAssResultBean assVbp(VbpStructure demo, VbpStructure project) {
        for (Field field : VbpStructure.class.getDeclaredFields()) {
            try {
                if (field.get(demo) != null && !(field.get(demo)).equals(field.get(project))) {
                    return new VBAssResultBean(false, ".vbp文件错误：" + field.getName());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                return new VBAssResultBean(false, ".vbp文件错误");
            }
        }
        return new VBAssResultBean(true, ".vbp文件正确");
    }

    // 判断.frm文件（设计题）
    private static VBDAssResultBean assFrmDe(FrmStructure demo, FrmStructure project) {
        for (FrmStructure.Component demoCom : demo.componentList) {
            boolean exist = false;
            boolean flag = false;
            for (FrmStructure.Component component : project.componentList) {
                // 组件类型与名称相同判断
                if (component.type.equals(demoCom.type) && component.name.equals(demoCom.name)) {
                    exist = true;
                    if (demoCom.caption != null) {
                        // 判断标题是否一致
                        flag = demoCom.caption.equalsIgnoreCase(component.caption.trim());
                        if (!flag) {
                            return new VBDAssResultBean(false, ".frm文件错误：组件\"" + demoCom.name + "\"caption属性错误", "0");
                        }
                    } else {
                        flag = true;
                    }
                }
            }
            if (!exist) {
                return new VBDAssResultBean(false, ".frm文件错误：组件\"" + demoCom.name + "\"不存在", "0");
            }
        }

        // 检验Sub

        //记录各Sub成绩
        List<Double> doubleList = new ArrayList<>(10);
        boolean exist = false;
        for (FrmStructure.Sub demoSub : demo.subList) {
            for (FrmStructure.Sub sub : project.subList) {
                if (demoSub.modifier.equals(sub.modifier) && demoSub.name.equals(sub.name)) {
                    if (exist) {
                        return new VBDAssResultBean(false, ".frm文件错误：Sub\"" + demoSub.modifier + " " + demoSub.name + "\"重复", "0");
                    }
                    exist = true;

                    // 先对demoSub进行分析
                    SubBodyInfo demoInfo = assGrammar(0, demoSub.body.replaceAll("\n", "").split(" "));
                    SubBodyInfo proInfo = assGrammar(0, sub.body.replaceAll("\n", "").split(" "));
                    VBDAssResultBean resultBean = compareSubBody(demoInfo, proInfo, 100);
                    doubleList.add(Double.valueOf(resultBean.getScore()));
                }
            }
            // 若不存在
            if (!exist) {
                return new VBDAssResultBean(false, ".frm文件错误：Sub\"" + demoSub.modifier + " " + demoSub.name + "\"不存在", "0");
            }
            exist = false;
        }

        double sum = 0;
        for (double e : doubleList) {
            sum += e;
        }

        return new VBDAssResultBean(true, "语法分析结果：" + sum / doubleList.size() * 0.3 + "（满分30分）", String.valueOf(sum / doubleList.size() * 0.3));
    }

    /**
     * 判断项目代码是否无误
     */
    static synchronized VBAssResultBean assProject(String vbpLocation) {

        VBAssResultBean bean = new VBAssResultBean(false);

        boolean res;

        File file = new File(errLocation);
        if (file.exists()) {
            res = file.delete();
            if (!res) {
                System.out.println("删除原错误日志失败（23）");
                bean.setResult("删除原错误日志失败（23）");
                return bean;
            }
        }
        try {
            res = file.createNewFile();
            if (!res) {
                System.out.println("创建错误日志失败（23）");
                bean.setResult("创建错误日志失败（23）");
                return bean;
            }

        } catch (IOException e) {
            e.printStackTrace();
            bean.setResult(e.getMessage());
            return bean;
        }

        BufferedReader reader = null;
        try {
            // 通过exec方法执行VB编译指令
            Process p = Runtime.getRuntime().exec(VB_PATH + "VB6 /make " + vbpLocation + " /out " + errLocation);
            // 线程阻塞，等待执行结束
            p.waitFor();
            // 以GBK编码的方式对日志进行读取
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            String tmp;
            int line = 0;
            StringBuilder content = new StringBuilder();
            while (null != (tmp = reader.readLine())) {
                if ("".equals(tmp.trim())) {
                    continue;
                }
                content.append(tmp);
                content.append("\n");
                line++;
            }
            bean.setResult("编译失败："+ content.toString());

            // 编译成功则只输出一行成功提示语，所以可判断编译成功
            if (line == 1) {
                bean.setStatus(true);
                return bean;
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            bean.setResult(e.getMessage());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bean;
    }

    private static SubBodyInfo assGrammar(int n, String[] demoBody) {
        SubBodyInfo subBodyInfo = new SubBodyInfo();

        Map<TYPE, Integer> params = subBodyInfo.getParams();
        SubBodyInfo.LOOP loop = null;
        SubBodyInfo.IF iff = null;
        while (n < demoBody.length) {
            System.out.println(demoBody[n]);
            switch (demoBody[n]) {
                // 变量
                case "As": {
                    // 将整型全化为Integer
                    if ("Byte".equals(demoBody[n + 1])
                            || "Single".equals(demoBody[n + 1])
                            || "Long".equals(demoBody[n + 1])) {
                        demoBody[n + 1] = "Integer";
                    }
                    TYPE type = TYPE.valueOf(demoBody[n + 1]);
                    System.out.println(type.name());
                    int count = 0;
                    int m = n - 1;
                    while (m > -1) {
                        try {
                            STATEMENT ty = STATEMENT.valueOf(demoBody[m]);
                            break;
                        } catch (IllegalArgumentException e) {
                            if (",".equals(demoBody[m])) {
                                m--;
                                continue;
                            }
                            count++;
                            m--;
                        }
                    }
                    params.put(type, params.get(type) == null ? count : (params.get(type) + count));
                    break;
                }
                //
                case "If":
                case "Select": {
                    int m = n - 1;
                    // 条件结尾
                    if (m > -1 && "End".equals(demoBody[m])) {

                        // 计算嵌套深度
                        if (iff.iff.size() == 0) {
                            iff.deep = 1;
                        } else {
                            int max = 0;
                            for (SubBodyInfo.IF son : iff.iff) {
                                if (max < son.deep) {
                                    max = son.deep;
                                }
                            }
                            iff.deep = max + 1;
                        }
                        subBodyInfo.switchStatics.merge(iff.deep, 1, Integer::sum);

                        // 回到father
                        if (iff.father != null) {
                            iff.father.iff.add(iff);
                            iff = iff.father;
                        } else {
                            subBodyInfo.getIfs().add(iff);
                            iff = null;
                        }
                    } else {
                        // 开始
                        if (iff == null) {
                            iff = subBodyInfo.new IF();
                        } else {
                            SubBodyInfo.IF tm = subBodyInfo.new IF();
                            tm.father = iff;
                            iff = tm;
                        }
                        iff.type = TSWITCH.valueOf(demoBody[n]);
                    }
                    break;
                }
                case "For":
                case "While":
                case "Do": {
                    // 开始
                    if (loop == null) {
                        loop = subBodyInfo.new LOOP();
                    } else {
                        SubBodyInfo.LOOP tm = subBodyInfo.new LOOP();
                        tm.father = loop;
                        loop = tm;
                    }
                    loop.type = TLOOP.valueOf(demoBody[n]);
                    break;
                }
                case "Next":
                case "Wend":
                case "Loop": {

                    // 计算嵌套深度
                    if (loop.loops.size() == 0) {
                        loop.deep = 1;
                    } else {
                        int max = 0;
                        for (SubBodyInfo.LOOP son : loop.loops) {
                            if (max < son.deep) {
                                max = son.deep;
                            }
                        }
                        loop.deep = max + 1;
                    }
                    // 回到father
                    subBodyInfo.loopStatics.merge(loop.deep, 1, Integer::sum);
                    if (loop.father != null) {
                        loop.father.loops.add(loop);
                        loop = loop.father;
                    } else {
                        subBodyInfo.getLoops().add(loop);
                        loop = null;
                    }
                }
            }
            n++;
        }
        return subBodyInfo;
    }

    /**
     * 过程体的对比 变量30%，判断35%，循环35%
     *
     * @param demo  标准代码
     * @param body  学生代码
     * @param score 总分
     * @return 结果
     */
    private static VBDAssResultBean compareSubBody(SubBodyInfo demo, SubBodyInfo body, float score) {

        /*********** 判断变量 ***********/
        int x = 0, y = 0;
        // count：判断数，right：答对数
        int count = 0, right = 0;

        // 对过程中不同类型的基础变量进行对比判断
        for (TYPE type : TYPE.values()) {
            count++;
            if (demo.getParams().get(type) == null) {
                x = 0;
            } else {
                x = demo.getParams().get(type);
            }
            if (body.getParams().get(type) == null) {
                y = 0;
            } else {
                y = body.getParams().get(type);
            }

            // 加减1用来容错
            if (Math.abs(x - y) < 2) {
                right++;
            }
        }
        // 变量判断分数
        double p1 = (right * 1.0 / (count * 1.0)) * 0.3;

        /*********** 判断loop ***********/
        right = 0;
        count = 0;
        int flag = 0;
        for (int n = 1; ; n++) {

            demo.loopStatics.putIfAbsent(n, 0);
            if (demo.loopStatics.get(n).equals(0)) {
                demo.loopStatics.put(n, 0);
                flag++;
                if (flag == 5) {
                    break;
                }
            } else {
                flag = 0;
            }
            body.loopStatics.putIfAbsent(n, 0);

            if (demo.loopStatics.get(n) != 0) {
                count++;
                if (demo.loopStatics.get(n).equals(body.loopStatics.get(n))) {
                    right++;
                }
            }
        }
        // 循环判断分数
        double p2 = 0.35;
        if (count != 0) {
            p2 = (right * 1.0 / (count * 1.0)) * 0.35;
        }

        /*********** 判断条件 ***********/
        right = 0;
        count = 0;
        flag = 0;
        for (int n = 1; ; n++) {

            demo.switchStatics.putIfAbsent(n, 0);
            if (demo.switchStatics.get(n).equals(0)) {
                demo.switchStatics.put(n, 0);
                flag++;
                if (flag == 5) {
                    break;
                }
            } else {
                flag = 0;
            }
            body.switchStatics.putIfAbsent(n, 0);

            if (demo.switchStatics.get(n) != 0) {
                count++;
                if (demo.switchStatics.get(n).equals(body.switchStatics.get(n))) {
                    right++;
                }
            }
        }

        // 条件判断分数
        double p3 = 0.35;
        if (count != 0) {
            p3 = (right * 1.0 / (count * 1.0)) * 0.35;
        }
        double realScore = Math.round((p1 + p2 + p3) * score);
        return new VBDAssResultBean(true, "", String.valueOf(realScore));
    }

    public static void main(String[] args) throws IOException, InterruptedException, AlgorithmException {

//        VBAssResultBean resultBean = analyzeVB("1523030059-prg4.zip", "prg4.zip", "0");
//        System.out.println(resultBean.isStatus() + ":" + resultBean.getResult());
//        ExecutorService executorService = Executors.newSingleThreadExecutor();
//        Callable<String> call = () -> {
//            // 开始执行耗时操作
//            Process p = Runtime.getRuntime().exec("/Applications/优酷.app/Contents/MacOS/优酷");
////            Process p = Runtime.getRuntime().exec(VB_PATH + "VB6 /make C:\\Users\\StupidZhe\\Desktop\\test\\工程1");
//            pp = p;
//            p.waitFor(5000, TimeUnit.MILLISECONDS);
//            return "执行成功";
//        };
//
//        try {
//            Future<String> future = executorService.submit(call);
//            String obj = future.get(5000, TimeUnit.MILLISECONDS); //任务处理超时时间设为 1 秒
//            System.out.println("任务成功返回:" + obj);
//        } catch (TimeoutException | ExecutionException ex) {
//            System.out.println("处理超时啦....");
//            pp.destroy();
//            ex.printStackTrace();
//            return;
//        } catch (Exception e) {
//            System.out.println("处理失败.");
//            e.printStackTrace();
//            return;
//        } finally {
//            // 关闭线程池
//            executorService.shutdown();
//        }
//        String content = "   Dim pw As String , i As Integer\n" +
//                "   If KeyAscii = 13 Then            \n" +
//                "      pw = Trim(Text1.Text)\n" +
//                "     If pw = \"1234567\" Or pw = \"1989643\" Or pw = \"1687799\" Then\n" +
//                "       Do \n" +
//                " While \n" +
//                "i = 1 \n" +
//                "Wend \n" +
//                "Loop \n" +
//                "      Select Case pw\n" +
//                "        Case \"1234567\"\n" +
//                "          Label2.Caption = \"ƒ„ «∆’Õ®”√ªß\"\n" +
//                "        Case \"1989643\"\n" +
//                "          Label2.Caption = \"ƒ„ « ⁄»®”√ªß\"\n" +
//                "        Case \"1687799\"\n" +
//                "          Label2.Caption = \"ƒ„ «Ãÿ–Ì”√ªß\"\n" +
//                "      End Select\n" +
//                "    Else                              '√‹¬Î≤ª’˝»∑\n" +
//                "      i = MsgBox(\"ø⁄¡Ó≤ª’˝»∑, «∑Ò÷ÿ ‘\", vbYesNo + vbQuestion, \"Ã· æ–≈œ¢\")\n" +
//                "      If i = vbYes Then Text1.Text = \"\": Text1.SetFocus Else End If\n" +
//                "    End If\n" +
//                "  End If".replaceAll("[(]", " ( ").
//                        replaceAll("[)]", " ) ").
//                        replaceAll("[+]", " + ").
//                        replaceAll("[-]", " - ").
//                        replaceAll("[*]", " * ").
//                        replaceAll("[/]", " / ").
//                        replaceAll("[=]", " = ").
//                        replaceAll("[,]", " , ").
//                        replaceAll("[)]", " ) ");
//        String tmp4 = "";
//        String[] tmp3 = content.split("[ ]");
//        for (String tmpp : tmp3) {
//            if (!"".equals(tmpp)) {
//                tmp4 += tmpp + " ";
//            }
//        }
//        assGrammar(0, tmp4.replaceAll("\n", "").split(" "));
//
//        HashMap<Integer, Integer> map = new HashMap<>(10);
//        for (int n = 0; n < 10; n++) {
//            System.out.println(map.get(n));
//        }

        double realScore = Math.round(1);
        System.out.println(String.valueOf(realScore));
    }


}
